package com.ptu.servlet;

import com.ptu.entity.PageTools;
import com.ptu.entity.Punishment;
import com.ptu.service.PunishmentService;
import com.ptu.service.impl.PunishmentServicelmpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/ptu/PunishmentServlet")
public class PunishmentServlet extends BaseServlet{
    private PunishmentService punishmentService = new PunishmentServicelmpl();
    private Punishment createpun(HttpServletRequest req, HttpServletResponse resp){
        Integer punid = toInteger(req.getParameter("punid"));
        String punname = req.getParameter("punname");
        Date puntime = toDate(req.getParameter("puntime"));
        String pundesc = req.getParameter("pundesc");
        String punstep = req.getParameter("punstep");

        Punishment pun = new Punishment(punid,punname,puntime,pundesc,punstep);
        return pun;
    }

    protected void punishmentSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        Punishment pun = createpun(req, resp);

        // 2. 数据的处理
        int save = punishmentService.addPunishment(pun);

        // 3. 给用户做出响应
        if (save > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/PunishmentServlet?method=showPunPage");
        } else {
            req.setAttribute("msg", "添加失败");
            req.getRequestDispatcher("/addPunishment.html").forward(req, resp);
        }
    }

    protected void punChange(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面中的数据
        Punishment punid = createpun(req, resp);

        // 2. 数据的处理
        int change = punishmentService.changePun(punid);

        // 3. 给用户做出响应
        if (change > 0) {
            resp.sendRedirect(req.getContextPath() + "/ptu/PunishmentServlet?method=showPunPage");
        } else {
            req.setAttribute("msg", "修改失败");
            req.getRequestDispatcher("/punUpdate.jsp").forward(req, resp);
        }
    }

    protected void removePun(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer punid = toInteger(req.getParameter("punid"));
        int i = punishmentService.removePun(punid);
        req.setAttribute("i", i);
        showPunPage(req, resp);
    }

    public void showPunPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 第一次访问的验证，如果没有传递参数，设置默认值
        String pageSizeReq = req.getParameter("pageSize");

        int pageSize = 2;   // 查询大小
        if(pageSizeReq != null && !pageSizeReq.equals("")){
            pageSize = Integer.parseInt(pageSizeReq);
        }

        String pageNumberStr = req.getParameter("pageNumber");
        int pageNumber = 1; // 开始值
        if(pageNumberStr!=null&&!pageNumberStr.equals("")){
            pageNumber = Integer.parseInt(pageNumberStr);
        }
        PageTools<Punishment> punAll = punishmentService.findPunAll(pageSize, pageNumber);
        req.setAttribute("punAll",punAll);
        req.getRequestDispatcher("/PunishmentList.jsp").forward(req, resp);

    }

    private void punFindAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 查询所有支出信息
        List<Punishment> punList = punishmentService.findAll();

        req.setAttribute("punList", punList);

        req.getRequestDispatcher("/PunishmentList.jsp").forward(req, resp);
    }

    protected void findPunMore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接受页面的数据
        String puntime = req.getParameter("puntime");

        String punname = req.getParameter("punname");

        // 2. 数据的处理

        // 查询符和条件集合
        List<Punishment> punMore = punishmentService.findPunMore(puntime, punname);
        PageTools<Punishment> punAll =new PageTools<>();
        // 3. 给用户做出响应
        punAll.setList(punMore);

        req.setAttribute("punAll", punAll);
        req.getRequestDispatcher("/PunishmentList.jsp").forward(req, resp);
    }

    protected void punFindOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取前端数据
        Integer punid = toInteger(req.getParameter("punid"));

        // 2. 数据处理
        Punishment pun = punishmentService.findPunByPid(punid);

        // 3. 返回结果

        if (pun != null) {
            req.setAttribute("pun", pun);
            req.setAttribute("punid", punid);
            req.getRequestDispatcher("/punUpdate.jsp").forward(req, resp);
        }
    }

    private Integer toInteger(String value) {
        return value == null ? null : Integer.parseInt(value);
    }

    private Date toDate(String value){
        try {
            return Date.valueOf(value);
        }catch (IllegalArgumentException i) {
            return null;
        }
    }
}
